# +----------------------------------------------------------------------
# | Pink [ A modern python web framework ]
# +----------------------------------------------------------------------
# | Copyright (c) 2023 http:#unnnnn.com All rights reserved.
# +----------------------------------------------------------------------
# | Author: chenjianhua <unnnnn@foxmail.com>
# +----------------------------------------------------------------------

import os
import shutil
from subprocess import call

from .Command import Command
from cleo.io.inputs.option import Option as option

class InstallCommand(Command):
    name = 'install'
    description = 'Installs all of  dependencies'
    options = [
        option('no-key', 'no_key', description='If set, pink install command will not generate and store a new key'),
        option('no-dev', 'no_dev', description='If set, app will install without dev'),
        option('force', 'f', description='Overwrite .env if exists',flag=True,default=False),
    ]
    """
    Installs all of  dependencies

    install
        {--no-key : If set, pink install command will not generate and store a new key}
        {--no-dev : If set, app will install without dev dependencies}
        {--f|--force : Overwrite .env if exists}
    """

    def handle(self):
        if not os.path.isfile(".env") or self.option("force"):
            shutil.copy(".env.example", ".env")

        if os.path.isfile("Pipfile"):
            try:
                if not self.option("no-dev"):
                    call(["pipenv", "install", "--dev"])
                else:
                    call(["pipenv", "install"])

                if not self.option("no-key"):
                    call(["pipenv", "shell", "new", "key", "--store"])

                return
            except Exception:
                self.comment(
                    """Pipenv could not install from your Pipfile .. reverting to pip installing requirements.txt"""
                )
                call(["python", "-m", "pip", "install", "-r", "requirements.txt"])
        elif os.path.isfile("requirements.txt"):
            call(["python", "-m", "pip", "install", "-r", "requirements.txt"])
        else:
            raise OSError("Could not find a Pipfile or a requirements.txt file")
        if not self.option("no-key"):
            try:
                self.call("key", "--store")
            except Exception:
                self.error(
                    "Could not successfully install app. This could happen for several reasons but likely because of how PinkPy is installed on your system and you could be hitting permission issues when PinkPy is fetching required modules."
                    " If you have correctly followed the installation instructions then you should try everything again but start inside an virtual environment first to avoid any permission issues. If that does not work then seek help in"
                    " the app Slack channel. Links can be found on GitHub in the main PinkPy repo."
                )
